package com.unism.licence.xml;

import org.jdom2.*;
import org.jdom2.input.*;
import java.io.FileInputStream;

public class ParseLicenceXmlFile {
	private Document doc ;
	private String projectName ;
	private String macTxt ;
	private String clientsTxt ;
	private String issueDtTxt ;
	private String validLifeTxt ;
	private String exponentHex ;
	private String modulusHex ;
	private String signedTxt ;
	/**
	 * 
	 * @return
	 */
	public Document createDom(FileInputStream in) {
		try {
			this.doc = new SAXBuilder().build(in);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return this.doc;
	}
	
	
	public boolean parseXml(){
		if (doc == null) {
			return false;
		}

		Element root = doc.getRootElement();
		if (root == null) {
			return false;
		}
		
		
		Element e = root.getChild("project");
		if (e == null) {
			return false;
		}
		this.projectName = e.getText().trim();
		if (this.projectName == null || this.projectName.equals("")) {
			return false;
		}
		
		
		e = root.getChild("mac");
		if (e == null) {
			return false;
		}
		this.macTxt = e.getText().trim();
		if (this.macTxt == null || this.macTxt.equals("")) {
			return false;
		}
		
		
		
		e = root.getChild("clients");
		if (e == null) {
			return false;
		}
		this.clientsTxt = e.getText().trim();
		if (this.clientsTxt == null || this.clientsTxt.equals("")) {
			return false;
		}
		
		
		
		e = root.getChild("issueDt");
		if (e == null) {
			return false;
		}
		this.issueDtTxt = e.getText().trim();
		if (this.issueDtTxt == null || this.issueDtTxt.equals("")) {
			return false;
		}
		
		
		
		e = root.getChild("validLife");
		if (e == null) {
			return false;
		}
		this.validLifeTxt = e.getText().trim();
		if (this.validLifeTxt == null || this.validLifeTxt.equals("")) {
			return false;
		}
		

		
		e = root.getChild("publicKey");
		if (e == null) {
			return false;
		}
		Element ee = e.getChild("exponent");
		if (ee == null) {
			return false;
		}
		this.exponentHex = ee.getText().trim();
		if (this.exponentHex == null || this.exponentHex.equals("")) {
			return false;
		}
		

		
		ee = e.getChild("modulus");
		if (ee == null) {
			return false;
		}
		this.modulusHex = ee.getText().trim();
		if (this.modulusHex == null || this.modulusHex.equals("")) {
			return false;
		}


		e = root.getChild("signed");
		if (e == null) {
			return false;
		}
		this.signedTxt = e.getText().trim();
		if (this.signedTxt == null || this.signedTxt.equals("")) {
			return false;
		}

		return true ;
	}


	public String getProjectName() {
		return projectName;
	}
	public String getMacTxt() {
		return macTxt;
	}
	public String getClientsTxt() {
		return clientsTxt;
	}
	public String getIssueDtTxt() {
		return issueDtTxt;
	}
	public String getValidLifeTxt() {
		return validLifeTxt;
	}
	public String getExponentHex() {
		return exponentHex;
	}
	public String getModulusHex() {
		return modulusHex;
	}
	public String getSignedTxt() {
		return signedTxt;
	}
	
}
